<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 200px"
        v-model="search.searchCode"
        placeholder="请输入入库单号"
      ></el-input>
      <el-select
        v-model="search.area"
        filterable
        remote
        center
        reserve-keyword
        placeholder="请输入区域"
        :remote-method="insideRemoteMethods"
        :loading="insideArea.loading"
      >
        <el-option
          v-for="item in insideArea.options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索供应商信息"
        v-model="supplierNameList.name"
      >
        <template slot="prepend">供应商</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="supplierBtn"
        ></el-button>
      </el-input>
      <el-input
        class="inp"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend">商品</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>
      <el-input
        class="inp"
        :disabled="true"
        placeholder="请搜索类别信息"
        v-model="classNameList.name"
      >
        <template slot="prepend">类别</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="classBtn"
        ></el-button>
      </el-input>
      <el-input
        class="inp"
        :disabled="true"
        placeholder="请搜索采购员信息"
        v-model="purchaseList.name"
      >
        <template slot="prepend">采购员</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="purchaseBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索仓库"
        v-model="warehouseList.name"
      >
        <template slot="prepend">仓库</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="warehouseBtn"
        ></el-button>
      </el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-button
      class="add"
      icon="el-icon-circle-plus"
      size="small"
      type="primary"
      @click="addPermission"
      >添加入库单</el-button
    >
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="50" align="center"></el-table-column> -->
      <!-- <el-table-column prop="bo_serialno" label="购货单单号" width="180" align="center"></el-table-column> -->
      <el-table-column
        prop="id"
        label="入库单单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusMsg"
        label="状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="warehouse_name"
        label="仓库"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="typeMsg"
        label="入库方式"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buyer"
        label="采购员"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="created_at"
        label="添加时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="exp_remark"
        label="备注"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="360px">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particulars(scope)"
            >详情</el-button
          >
          <el-button
            size="mini"
            v-if="scope.row.status == 1"
            icon="el-icon-edit-outline"
            @click="confirms(scope)"
            >审核</el-button
          >
          <el-button
            size="mini"
            v-if="scope.row.status == 1"
            icon="el-icon-edit"
            type="primary"
            @click="handleEdit(scope)"
            >编辑</el-button
          >

          <el-button
            size="mini"
            v-if="scope.row.status == 1"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <div style="height: 0">
      <purchaseDialog
        :purchasedialog="purchasedialog"
        @tag="purchaseTag"
      ></purchaseDialog>
      <supplierSearch
        :supplierdialog="supplierdialog"
        @tag="supplierTag"
      ></supplierSearch>
      <commodityDialog
        :commoditydialog="commoditydialog"
        @tag="commodityTag"
      ></commodityDialog>
      <classDialog :classdialog="classdialog" @tag="classTag"></classDialog>
      <!-- dialog -->
      <addPutStorageDialog
        :dialog="dialog"
        @upData="getData"
        @upFormData="upFormData"
      ></addPutStorageDialog>
      <!-- 详情Dailog -->
      <particularsDialog
        @upData="getData"
        :particularsDialogs="particularsDialogs"
      ></particularsDialog>
      <!-- 编辑Dailog -->
      <compileShoppingDialog
        @upData="getData"
        :compileDialog="compileDialog"
      ></compileShoppingDialog>
      <warehouseselect
        :warehouseselect="warehouseselect"
        @tag="warehouseTag"
      ></warehouseselect>
    </div>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import { parseTime, getnowDate } from "@/utils/index";
import ParticularsDialog from "@/components/putStorage/giftParticularsDialog";
import AddPutStorageDialog from "@/components/putStorage/giftAddPutStorageDialog";
import Pagination from "@/components/pagination";
import CompileShoppingDialog from "@/components/putStorage/giftCompileShoppingDialog";
import PurchaseDialog from "@/components/grossMargin/purchaseDialog";
import SupplierSearch from "@/components/grossMargin/supplierSearch";
import CommodityDialog from "@/components/grossMargin/commodityDialog";
import ClassDialog from "@/components/grossMargin/classDialog";
import { getAreaList } from "@/api/permission/adminlist";
import Warehouseselect from "@/components/commodity/warehouseselect";
import {
  putStotageList,
  deletes,
  checkStorageBill,
} from "@/api/putStorage/giftPutStotageList";
export default {
  components: {
    pagination: Pagination,
    addPutStorageDialog: AddPutStorageDialog,
    particularsDialog: ParticularsDialog,
    compileShoppingDialog: CompileShoppingDialog,
    purchaseDialog: PurchaseDialog,
    supplierSearch: SupplierSearch,
    commodityDialog: CommodityDialog,
    classDialog: ClassDialog,
    warehouseselect: Warehouseselect,
  },
  data() {
    return {
      //默认时间
      normal_startime: getnowDate().startimer,
      normal_endtime: getnowDate().endtimer,
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      //dialog信息
      dialog: {
        centerDialogVisible: false,
        dialogType: "",
        id: "",
      },
      compileDialog: {
        centerDialogVisible: false,
        dialogType: "",
        id: "",
      },
      search: {
        searchCode: "",
        start_time: "",
        end_time: "",
        area: "",
      },
      insideArea: {
        innerVisible: false,
        options: [],
        value: "",
        list: [],
        loading: false,
      },
      particularsDialogs: {
        centerDialogVisible: false,
        id: "",
      },
      //dialog信息
      supplierdialog: {
        centerDialogVisible: false,
      },
      commoditydialog: {
        centerDialogVisible: false,
      },
      purchasedialog: {
        centerDialogVisible: false,
      },
      classdialog: {
        centerDialogVisible: false,
      },
      warehouseselect: {
        centerDialogVisible: false,
      },
      supplierNameList: {
        name: "",
        id: "",
      },
      commodityNameList: {
        name: "",
        id: "",
      },
      purchaseList: {
        name: "",
        code: "",
      },
      classNameList: {
        name: "",
        id: "",
      },
      warehouseList: {
        name: "",
        code: "",
      },
      date: [],
    };
  },
  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    this.getData();
    this.getDatas();
  },
  methods: {
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        warehouse_code: this.warehouseList.code,
        productclass_id_str: this.classNameList.id,
        goods_id_str: this.commodityNameList.id,
        buyer_code_str: this.purchaseList.code,
        supplier_id_str: this.supplierNameList.id,
        serial_code: this.search.searchCode,
        start_date: this.search.start_time,
        end_date: this.search.end_time,
        orgarea_code_str: this.search.area,
      };

      putStotageList(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.data.forEach((res) => {
              switch (res.type) {
                case 10:
                  res.typeMsg = "购货入库";
                  break;
                case 40:
                  res.typeMsg = "行采入库";
                  break;
                case 50:
                  res.typeMsg = "赠品入库";
                  break;
              }
              res.statusMsg = res.status == 2 ? "已审核" : "未审核";
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getDatas() {
      let data = {
        page: 1,
        limit: 50,
      };

      //获取初始化区域列表
      getAreaList(data).then((res) => {
        this.insideArea.options = res.data.listdata.map((item) => {
          return { value: `${item.code}`, label: `${item.name}` };
        });
      });
    },
    // 搜索区域
    insideRemoteMethods(query) {
      if (query !== "") {
        this.getSupplierLists(query);
      } else {
        this.insideArea.options = [];
      }
    },
    getSupplierLists(query) {
      let data = {
        name: query,
        page: 1,
        limit: 100,
      };
      getAreaList(data).then((res) => {
        this.insideArea.list = res.data.listdata.map((item) => {
          return { value: `${item.code}`, label: `${item.name}` };
        });
        this.insideArea.options = this.insideArea.list.filter((item) => {
          this.insideArea.loading = false;
          return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        });
      });
    },
    //添加入库单
    addPermission() {
      this.dialog.centerDialogVisible = true;
      this.dialog.dialogType = "";
    },
    //修改入库单
    handleEdit(scope) {
      this.compileDialog.centerDialogVisible = true;
      this.compileDialog.id = scope.row.id;
      this.compileDialog.dialogType = "edit";
    },
    //详情
    particulars(scope) {
      this.particularsDialogs.centerDialogVisible = true;
      this.particularsDialogs.id = scope.row.id;
    },
    //删除
    handleDelete(scope) {
      let data = {
        ids: scope.row.id,
      };
      this.$confirm("是否删除该条信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deletes(data).then((res) => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //审核
    confirms(scope) {
      let data = {
        ids: scope.row.id,
      };

      this.$confirm("是否审核信息?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          checkStorageBill(data).then((res) => {
            if (res.code == 0) {
              this.getData();
              this.$message({
                type: "success",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消审核",
          });
        });
    },
    //dialog触发该函数。关闭dialog,在没对表单重置之前先清空表单。
    upFormData() {
      this.formData = {
        id: "",
        name: "",
        grouping: "",
        groupingCode: "",
        getController: "",
        getActions: "",
      };
    },
    //超级供应商搜索
    supplierBtn() {
      this.supplierdialog = {
        centerDialogVisible: true,
      };
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true,
      };
    },
    //超级采购员搜索
    purchaseBtn() {
      this.purchasedialog = {
        centerDialogVisible: true,
      };
    },
    //超级类别搜索
    classBtn() {
      this.classdialog = {
        centerDialogVisible: true,
      };
    },
    //超级仓库搜索
    warehouseBtn() {
      this.warehouseselect = {
        centerDialogVisible: true,
      };
    },
    supplierTag(data) {
      this.supplierNameList = {
        name: "",
        id: "",
      };

      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.supplierNameList.name = arrName.join(",");
      this.supplierNameList.id = arrCode.join(",");
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        id: "",
      };

      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.id = arrCode.join(",");
    },
    purchaseTag(data) {
      this.purchaseList = {
        name: "",
        code: "",
      };

      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.purchaseList.name = arrName.join(",");
      this.purchaseList.code = arrCode.join(",");
    },
    classTag(data) {
      this.classNameList = {
        name: "",
        id: "",
      };

      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.classNameList.name = arrName.join(",");
      this.classNameList.id = arrCode.join(",");
    },
    warehouseTag(data) {
      this.warehouseList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.warehouseList.name = arrName.join(",");
      this.warehouseList.code = arrCode.join(",");
    },
    // 搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");

          if (this.date.length > 0) {
            this.search.start_time = this.date[0];
            this.search.end_time = this.date[1];
          }
          link.href =
            this.$axios.defaults.baseURL +
            "/api/storagebill/exportGiftStorageBill?token=" +
            getToken() +
            "&productclass_id_str=" +
            this.classNameList.id +
            "&warehouse_code=" +
            this.warehouseList.code +
            "&goods_id_str=" +
            this.commodityNameList.id +
            "&buyer_code_str=" +
            this.purchaseList.code +
            "&supplier_id_str=" +
            this.supplierNameList.id +
            "&serial_code=" +
            this.search.searchCode +
            "&start_date=" +
            this.search.start_time +
            "&end_date=" +
            this.search.end_time +
            "&orgarea_code_str=" +
            this.search.area;

          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //清空
    empty() {
      this.date = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        searchCode: "",
        start_time: this.normal_startime,
        end_time: this.normal_endtime,
        area: "",
      };
      this.insideArea = {
        innerVisible: false,
        options: [],
        value: "",
        list: [],
        loading: false,
      };
      this.getDatas();
      this.supplierNameList = {
        name: "",
        id: "",
      };
      this.commodityNameList = {
        name: "",
        id: "",
      };
      this.purchaseList = {
        name: "",
        code: "",
      };
      this.classNameList = {
        name: "",
        id: "",
      };
      this.warehouseList.code = "";
      this.warehouseList.name = "";
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  margin-top: 30px;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 20px;
  top: 10px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
.inp {
  margin-bottom: 5px;
  width: 300px;
}
</style>